package ru.urfc.portal.dao.filter;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import ru.urfc.portal.exceptions.InternalException;

public class VariablesHolder {

	private static final String NAME_PREFIX = "v";
    private long generatedNamesCount = 0;
    private Map<String, Object> variables = new HashMap<String, Object>();
    
	public Object get(String key) {
        return variables.get(key);
    }
    
    public Set<String> getKeySet() {
        return variables.keySet();
    }

    public String put(Object value) {
        String key = generateName();
        check(key);
        variables.put(key, value);
        return key;
    }

	public static class KeyWasAlreadyAddedException extends InternalException {
		public KeyWasAlreadyAddedException(String messageTemplate, Object... arguments) {
			super(messageTemplate, arguments);
		}		
	}
    
    private void check(String key) {
        if (variables.containsKey(key)) {
            throw new KeyWasAlreadyAddedException("Key {0} was already added", key);
        }
    }
    
    public String generateName() {
        return NAME_PREFIX + ++generatedNamesCount;
    }

}
